﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace SAP
{
    public partial class Administration : System.Web.UI.Page
    {
        private LoginSession CurrentLoginSession;
        public void SessionCheck()
        {

            if (Session["LoginSession"] != null)
            {
                this.CurrentLoginSession = (LoginSession)Session["LoginSession"];
                GF = new GeneralFunctions(CurrentLoginSession.UserName);
            }
            else
            {
                Response.Redirect("SessionErrorPage.aspx");
            }

        }    

        protected void Page_Load(object sender, EventArgs e)
        {
            SessionCheck();
            

            if (!(String.IsNullOrEmpty(txtPassword.Text.Trim())))
            {
                txtPassword.Attributes["value"] = txtPassword.Text;
            }

            if (!IsPostBack)
            {
                try
                {

                    if (!(String.IsNullOrEmpty(txtPassword.Attributes["value"])))
                    {
                        txtPassword.Text = txtPassword.Attributes["value"].ToString();
                    }
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "ShowLoader", "Dialog.showLoader();", true);

                    MasterData masterDataWS = new MasterData();
                    loadUserDefinedTable();
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading",
                                                            "Dialog.hideLoader();", true);
                }
                catch (Exception ex)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors",
                                                           "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ex.ToString()) + "','');", true);

                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading",
                                                        "Dialog.hideLoader();", true);
                }
            }
        }

        protected void btnCreateUDF_Click(object sender, EventArgs e)
        {
            GetDefault df = new GetDefault();
            df.CreateUDF(CurrentLoginSession.UserName);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string connectionString = "Server=" + txtServer.Text + ";Database=" + txtDBName.Text + ";User Id=" + txtUsername.Text + ";Password=" + txtPassword.Text + ";";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + "Connection ok!" + "','');", true);
                }
                catch (SqlException)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + "Cannot establish the connection." + "','');", true);
                }
                finally
                {
                    // not really necessary
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading",
                                                        "Dialog.hideLoader();", true);
                    connection.Close();

                }
            }
        }

        protected void btn_SaveConnection_Click(object sender, EventArgs e)
        {
            //Server=.;Database=SBODemoSG;uid=sa;pwd=admin2013~;
            System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            System.Configuration.ConnectionStringsSection section = (System.Configuration.ConnectionStringsSection)config.GetSection("connectionStrings");

            string newConnectionString = "Server=" + txtServer.Text + ";Database=" + txtDBName.Text + ";User Id=" + txtUsername.Text + ";Password=" + txtPassword.Text + ";";
            section.ConnectionStrings[0].ConnectionString = newConnectionString;
            config.Save();
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + "Operation complete sucessful!" + "','');", true);
        }
        private GeneralFunctions GF;
        //load all user define tables
        private void loadUserDefinedTable() {
            string sqlStr = "select * from sys.Tables where name like '@%'";
            DataTable tbTable =  GF.RunQuery(sqlStr);
            lvContents.DataSource = tbTable;
            lvContents.DataBind();
        }
    }
}